package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.framework.web.AjaxResult;
import com.example.demo.utils.HttpUtil;
import com.example.demo.utils.SignUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 客户端
 *
 * @author : SPL
 * @since : 2020-04-29 11:31
 **/
@RequestMapping("/client")
@RestController
public class ClientController {


    @RequestMapping("/send1")
    public Object send1() {

        Map<String, String> param = new HashMap<>();
        param.put("userId", "9527");
        param.put("amount", "9.99");
        param.put("productId", "9885544154");


        Map<String, String> headers = new HashMap<>(4);
        headers.put("appid", "9527");
        headers.put("timestamp", String.valueOf(new Date().getTime()));
        headers.put("nonce", "9885544154");
        String sign = SignUtil.getSign(headers,"mysecret123456");
        headers.put("sign", sign);

        try {
            String res = HttpUtil.doGet("http://127.0.0.1:8083/server/test", param, headers);
            System.out.println(res);
            AjaxResult jsonRes = JSON.parseObject(res, AjaxResult.class);
            return jsonRes;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/send2")
    public Object send2() {

        Map<String, String> param = new HashMap<>();
        param.put("userId", "9527");
        param.put("amount", "9.99");
        param.put("productId", "9885544154");


        Map<String, String> headers = new HashMap<>(4);
        headers.put("appid", "9527");
        headers.put("timestamp", String.valueOf(new Date().getTime()));
        headers.put("nonce", "9885544154");
        String sign = SignUtil.getSign(headers,"mysecret123456");
        headers.put("sign", sign);

        try {
            String res = HttpUtil.doPost("http://127.0.0.1:8083/server/test", param, headers);
            System.out.println(res);
            AjaxResult jsonRes = JSON.parseObject(res, AjaxResult.class);
            return jsonRes;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/send3")
    public Object send3() {

        Map<String, String> param = new HashMap<>();
        param.put("userId", "9527");
        param.put("amount", "9.99");
        param.put("productId", "9885544154");


        Map<String, String> headers = new HashMap<>(4);
        headers.put("appid", "9527");
        headers.put("timestamp", String.valueOf(new Date().getTime()));
        headers.put("nonce", "9885544154");
        String sign = SignUtil.getSign(headers,"mysecret123456");
        headers.put("sign", sign);

        try {
            String res = HttpUtil.doJsonPost("http://127.0.0.1:8083/server/test", JSON.toJSONString(param), headers);
            System.out.println(res);
            AjaxResult jsonRes = JSON.parseObject(res, AjaxResult.class);
            return jsonRes;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
